/*
 * Saida.java
 *
 * Created on 11 de Novembro de 2008, 16:04
 */
package botoseis.mainGui.workflows;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import botoseis.mainGui.workflows.WorkflowJob;

/**
 *
 * @author  gabriel
 */
public class WorkflowLogDlg extends javax.swing.JFrame {

    String status;
    private String inputFile;

    /** Creates new form Saida */
    public WorkflowLogDlg() {
        initComponents();
        setTitle("Output Flow");
        taOutput.setLineWrap(true);
        taOutput.setWrapStyleWord(true);
    }

    public WorkflowLogDlg(String path) {
        initComponents();
        setTitle("Output Flow");
        taOutput.setLineWrap(true);
        taOutput.setWrapStyleWord(true);
        loadFromFile(path);
    }

    public void carregarLog(String dir) {
    }

    public boolean checkError() {
        if (taOutput.getText().toLowerCase().contains(".c")  || taOutput.getText().toLowerCase().contains("error") || taOutput.getText().toLowerCase().contains("exception") || taOutput.getText().toLowerCase().contains("can't")) {
            return true;
        } else {
            return false;
        }
    }

    public void salveLog(String string) {
        try {
            BufferedWriter buff = new BufferedWriter(new FileWriter(string));
            buff.write(taOutput.getText());
            buff.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void loadFromFile(String path) {

        if(path == null){
            taOutput.setText("");
            return;
        }
        try {
            BufferedReader buff = new BufferedReader(new FileReader(path));

            String linha = "";
            taOutput.setText("");
            while ((linha = buff.readLine()) != null) {
                taOutput.append(linha + "\n");
            }

            buff.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        taOutput = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mSave = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mMonitor = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        taOutput.setColumns(20);
        taOutput.setEditable(false);
        taOutput.setRows(5);
        taOutput.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                taOutputPropertyChange(evt);
            }
        });
        jScrollPane1.setViewportView(taOutput);

        jMenu1.setText("File");

        mSave.setText("Save as");
        mSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mSaveActionPerformed(evt);
            }
        });
        jMenu1.add(mSave);
        jMenu1.add(jSeparator1);

        mMonitor.setText("Close");
        mMonitor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mMonitorActionPerformed(evt);
            }
        });
        jMenu1.add(mMonitor);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 507, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 583, Short.MAX_VALUE)
                .addGap(28, 28, 28))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    close(evt);
}//GEN-LAST:event_formWindowClosing

private void mMonitorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mMonitorActionPerformed
    dispose(evt);
}//GEN-LAST:event_mMonitorActionPerformed

private void taOutputPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_taOutputPropertyChange
}//GEN-LAST:event_taOutputPropertyChange

private void mSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mSaveActionPerformed
    JFileChooser jfc = new JFileChooser();

    jfc.setFileSelectionMode(JFileChooser.SAVE_DIALOG);

    if (jfc.showSaveDialog(mSave) == JFileChooser.APPROVE_OPTION) {
        File file = jfc.getSelectedFile();
        salveLog(file.getAbsolutePath());
    }
}//GEN-LAST:event_mSaveActionPerformed

    public void append(String agrs) {
        taOutput.append(agrs);
        taOutput.setCaretPosition(taOutput.getText().length());
    }

    public void close(java.awt.event.WindowEvent evt) {
    }

    public void dispose(java.awt.event.ActionEvent evt) {
        dispose();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JMenuItem mMonitor;
    private javax.swing.JMenuItem mSave;
    public javax.swing.JTextArea taOutput;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the inputFile
     */
    public String getInputFile() {
        return inputFile;
    }

    /**
     * @param inputFile the inputFile to set
     */
    public void setInputFile(String inputFile) {
        this.inputFile = inputFile;
    }
}
